<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          :show="false"
          :showInput="showInput"
          @search="handleSearch"
        />

        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>

        <el-button type="primary" class="ml10" @click="handleSearch"
          >搜索</el-button
        >
        <el-button type="primary" @click="handleExport">导出</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="index"
          label="排序"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="subDistrict"
          align="center"
          label="街道"
        ></el-table-column>
        <el-table-column
          prop="communityName"
          align="center"
          label="社区"
        ></el-table-column>
        <el-table-column
          prop="villageName"
          align="center"
          label="小区"
        ></el-table-column>
        <el-table-column
          prop="weight"
          align="center"
          label="总重量(kg)"
        ></el-table-column>
        <el-table-column
          prop="frequency"
          align="center"
          label="总次数"
        ></el-table-column>
        <el-table-column
          prop="perishableWeight"
          label="易腐重量(kg)"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="perishableFrequency"
          label="易腐次数"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="recyclablesWeight"
          label="可回收重量(kg)"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="recyclablesFrequency"
          label="可回收次数"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="otherWeight"
          label="其他重量(kg)"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="otherFrequency"
          label="其他次数"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="harmfulWeight"
          label="有害重量(kg)"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="harmfulFrequency"
          label="有害次数"
          align="center"
        ></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import NProgress from "nprogress";
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
export default {
  name: "collectionAndTransportationStatistics",
  data() {
    return {
      query: {
        pageIndex: 1,
        pageSize: 20,
        streetId: "",
        streetName: "",
        communityId: "",
        communityName: "",
        villageId: "",
        villageName: "",
      },
      showInput: true,
      value2: "",
      streetList: [],
      communityList: [],
      villageList: [],
      tableData: [],
      pageTotal: 0,
    };
  },
  created() {
    this.getData();
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  methods: {
    ...commonItem1,
    getData() {
      if (
        !Boolean(this.query.streetId) &&
        this.query.villageName.trim() == ""
      ) {
        return this.$message.warning({
          message: "请选择查询的街道,社区,小区",
          duration: 1500,
        });
      }
      if (!this.value2) {
        return this.$message.warning({
          message: "请选择查询日期",
          duration: 1500,
        });
      }
      NProgress.start();
      this.$axios
        .get("/village/dataStatistice/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            createDate1: this.value2[0].getTime().toString(),
            createDate2: (
              this.value2[1].getTime() +
              (24 * 3600 * 1000 - 1000)
            ).toString(),
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            villageName:
              this.showInput == false ? "" : this.query.villageName.trim(),
          },
        })
        .then(({ data: res }) => {
          console.log("收运数据统计", res);
          if (res.code !== 0) {
            NProgress.done();
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.tableData.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
          this.pageTotal = res.data.total;
          NProgress.done();
        })
        .catch((err) => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    handleExport() {
      if (
        !Boolean(this.query.streetId) &&
        this.query.villageName.trim() == ""
      ) {
        return this.$message.warning({
          message: "请选择查询的街道,社区,小区",
          duration: 1500,
        });
      }
      if (!this.value2) {
        return this.$message.warning({
          message: "请选择导出日期",
          duration: 1500,
        });
      }
      NProgress.start();
      this.$axios
        .get("/village/dataStatistice/export", {
          params: {
            createDate1: this.value2[0].getTime().toString(),
            createDate2: (
              this.value2[1].getTime() +
              (24 * 3600 * 1000 - 1000)
            ).toString(),
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            villageName:
              this.showInput == false ? "" : this.query.villageName.trim(),
          },
          responseType: "blob",
        })
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);

          link.setAttribute(
            "download",
            `${this.query.streetName}_${this.query.communityName}_${this.query.villageName}.xls`
          );

          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          NProgress.done();
        })
        .catch((err) => {});
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.ml10 {
  margin-left: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
